<?php



namespace App\Zesow;

use App\Constant\CacheConstant;
use App\Services\Base\Interfaces\CacheServiceInterface;
use App\Services\Base\Services\ConfigService;
use App\Services\Base\Interfaces\ConfigServiceInterface;
use GuzzleHttp\Client;
use http\Exception;

class YingShi
{
    protected  $apiAccessToken = null;
    protected  $baseUrl =  'https://open.ys7.com';

    protected  $client = null;


    /**
     * @var ConfigService
     */
    protected $configService;

    public function __construct()
    {
        $this->apiAccessToken = $this->getAccessToken();
        $this->client = new Client();
    }


    /**
     * @return \EasyWeChat\OfficialAccount\Application|null
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */

    protected function getApiAccessToken(): string
    {
        return $this->apiAccessToken ?? $this->apiAccessToken = $this->getAccessToken();
    }
    public  function  getAccessToken()
    {


        $cacheService = app()->make(CacheServiceInterface::class);
        $cacheKey = get_cache_key(CacheConstant::YING_SHI_APP['name']);
        if ($cacheService->has($cacheKey)) {
            $token =  $cacheService->get($cacheKey);
        }

        $configService = app()->make(ConfigServiceInterface::class);
        $configs = $configService->getYingshiConfig();
        $client = new Client();
        $response = $client->post($this->baseUrl.'/api/lapp/token/get', [
            'form_params' => [
                'appKey' => $configs['app_key'],
                'appSecret' => $configs['secret']
            ],
        ]);
        $body = $response->getBody()->getContents();
        $result = json_decode($body, true);
        if ($result['code']==200) {
            $token =$result['data']['accessToken'];
        }
        $cacheService->put(get_cache_key(CacheConstant::YING_SHI_APP['name']),$token, CacheConstant::YING_SHI_APP['expire']);
        return $token;
    }

    public  function add(string $serialNumber,string $validateCode)
    {


        $response =$this->client->post($this->baseUrl.'/api/lapp/device/add', [
            'form_params' => [
                'accessToken' => $this->getAccessToken(),
                'deviceSerial' => $serialNumber,
                'validateCode'=>$validateCode
            ],
        ]);
        $body = $response->getBody()->getContents();
        return json_decode($body, true);

    }
       public  function delete(string $serialNumber)
    {


        $response =$this->client->post($this->baseUrl.'/api/lapp/device/delete', [
            'form_params' => [
                'accessToken' => $this->getAccessToken(),
                'deviceSerial' => $serialNumber,
            ],
        ]);
        $body = $response->getBody()->getContents();
        return json_decode($body, true);



    }

    public  function status(string $serialNumber)
    {


        $response = $this->client->post($this->baseUrl . '/api/v3/device/searchDeviceInfo', [
            'form_params' => [
                'accessToken' => $this->getAccessToken(),
                'deviceSerial' => $serialNumber,
            ],
        ]);
        $body = $response->getBody()->getContents();
        $result = json_decode($body, true);
        return $result['result']['data'] ?? '';


    }





    }
